package com.threeox.httplibrary.callback;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;

import okhttp3.Response;

/** 
 *
 * @ClassName: GenericsCallback 
 *
 * @Description: Todo(自定义回调)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/21 11:35 
 * 
 * @version 1.0
 */
public abstract class GenericsCallback<T> extends Callback<T> {

    IGenericsSerializator mGenericsSerializator;

    public GenericsCallback(IGenericsSerializator serializator) {
        mGenericsSerializator = serializator;
    }

    @Override
    public T parseNetworkResponse(Response response, int id, String url) throws IOException {
        String string = response.body().string();
        Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        if (entityClass == String.class) {
            return (T) string;
        }
        T bean = mGenericsSerializator.transform(string, entityClass);
        return bean;
    }

}
